let gulp = require("gulp");
let babel = require("gulp-babel");
let uglify = require("gulp-uglify");
let csso = require("gulp-csso");
let watch = require("gulp-watch");
let webserver = require("gulp-webserver");
let sass = require("gulp-sass");

//启动服务器前先加载static文件
gulp.task("server",["static","compileHtml","compileJs","compileCss"],function(){
	gulp.src("dist")  //读取数据
		.pipe(
			webserver({
				livereload:true,
				//open : true,
				proxies:[
					/*{
						source : "/name",
						target : "https://shopapi.smartisan.com/product/home"
					},*/
					{
						source : "/home",
						target : "http://localhost:8000/pages/index"
					},
					/*{
						source : "/home",
						target : "https://shopapi.smartisan.com/product/promotions?with_num=true"
					}*/
				]
			}))
	gulp.watch("src/pages/**/*.html",["compileHtml"])
	gulp.watch("src/styles/**/*.scss",["compileCss"])
	gulp.watch("src/script/**/*.js",["compileJs"])
	gulp.watch("src/conf/**/*.js",["compileJs"])
	gulp.watch("src/pages/**/*.js",["compileJs"])
})

//读取static文件传送到dist下的static中
gulp.task("static",function(){
	gulp.src("src/static/**/*.*")
		.pipe(gulp.dest("dist/static/"))
});

//读取html文件到dist中的pages下
gulp.task("compileHtml",function(){
	gulp.src("src/pages/**/*.html")
		.pipe(gulp.dest("dist/pages/"))
});


//读取js文件到dist中的pages下
gulp.task("compileJs",function(){
	gulp.src("src/pages/**/*.js")
		.pipe(babel({
			presets:['@babel/env']
		}))
		.pipe(uglify())  //将js文件压缩
		.pipe(gulp.dest("dist/pages/"))
	
	gulp.src("src/script/**/*.js") //读取自定义模块的js文件
		.pipe(babel({
			presets:['@babel/env']
		}))
		//.pipe(uglify())  //将js文件压缩
		.pipe(gulp.dest("dist/scripts/"))
		
	gulp.src("src/conf/**/*.js") //读取自定义模块的js文件
		.pipe(babel({
			presets:['@babel/env']
		}))
		.pipe(uglify())  //将js文件压缩
		.pipe(gulp.dest("dist/conf/"))
})

gulp.task("compileCss",function(){ //监听css文件
	gulp.src("src/styles/**/*.scss")
		.pipe(sass().on('error',sass.logError))
		.pipe(gulp.dest("dist/styles/"))
});
